package com.ouheng.shop.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ouheng.common.utils.PageUtils;
import com.ouheng.common.utils.Query;
import com.ouheng.common.utils.R;
import com.ouheng.common.utils.ServletsUtils;
import com.ouheng.shop.domain.CouponDO;
import com.ouheng.shop.domain.VolumeActivityDO;
import com.ouheng.shop.service.VolumeActivityService;

/**
 * 
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2018-09-10 15:08:44
 */
 
@Controller
@RequestMapping("/shop/volumeActivity")
public class VolumeActivityController {
	@Autowired
	private VolumeActivityService volumeActivityService;
	
	@GetMapping()
	String VolumeActivity(){
	    return "shop/volumeActivity/volumeActivity";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<VolumeActivityDO> volumeActivityList = volumeActivityService.list(query);
		int total = volumeActivityService.count(query);
		PageUtils pageUtils = new PageUtils(volumeActivityList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){
	    return "shop/volumeActivity/add";
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Long id,Model model){
		VolumeActivityDO volumeActivity = volumeActivityService.get(id);
		model.addAttribute("volumeActivity", volumeActivity);
	    return "shop/volumeActivity/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( VolumeActivityDO volumeActivity,HttpServletRequest request){
		
		
		List<Object> list;
		try {
			list = ServletsUtils.convertObjectList(CouponDO.class, request,"attachs_");
			
			
			if(volumeActivityService.save(volumeActivity,list)>0){
				return R.ok();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
			
		
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( VolumeActivityDO volumeActivity){
		volumeActivityService.update(volumeActivity);
		return R.ok();
	}
	
	@ResponseBody
	@RequestMapping("/stop")
	public R stop( VolumeActivityDO volumeActivity){
		volumeActivityService.stop(volumeActivity);
		return R.ok();
	}
	
	@ResponseBody
	@RequestMapping("/start")
	public R start( VolumeActivityDO volumeActivity){
		volumeActivityService.start(volumeActivity);
		return R.ok();
	}
	
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Long id){
		if(volumeActivityService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Long[] ids){
		volumeActivityService.batchRemove(ids);
		return R.ok();
	}
	
}
